/* eslint-disable react/prefer-stateless-function */
import React, { Component } from 'react';
import { Form, Button } from 'antd';

class CustomButton extends Component {
  render() {
    const { onChange, value } = this.props;
    // onChange和value都接收由rc-form传来的值，然后在组件内部得到值时，将值作为参数调用onChange,从而获得组件的值。
    return (
      <div>
        <p
          style={{
            border: '1px solid red',
          }}
          onClick={() => {
            onChange(1);
          }}
        >
          1
        </p>
        <p
          style={{
            border: '1px solid red',
          }}
          onClick={() => {
            onChange(2);
          }}
        >
          2
        </p>
        <p>结果:{value}</p>
      </div>
    );
  }
}

@Form.create()
class AntdFormDemo extends Component {
  render() {
    const {
      form: { getFieldDecorator, validateFields },
    } = this.props;
    return (
      <div>
        <Form
          onSubmit={e => {
            e.preventDefault();
            validateFields((err, val) => {
              console.log(val);
            });
          }}
        >
          <Form.Item>{getFieldDecorator('res', {})(<CustomButton></CustomButton>)}</Form.Item>
          <Form.Item>
            <Button type="primary" htmlType="submit">
              测试
            </Button>
          </Form.Item>
        </Form>
      </div>
    );
  }
}

export default AntdFormDemo;
